﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Acme.BookStore2.Permissions;
using Microsoft.AspNetCore.Authorization;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Services;
using Volo.Abp.ObjectExtending;

namespace Acme.BookStore2
{
    public class BlobAppService : ApplicationService, IBlobAppService
    {
        private readonly BlobManager _blobManager;

        public BlobAppService(BlobManager blobManager)
        {
            _blobManager = blobManager;
        }

        public async Task<BlobDto> CreateAsync(CreateBlobInput input)
        {
            var folderPath = DateTime.Now.ToString("yyyy-MM-dd");
            var extension = Path.GetExtension(input.FileName);
            var blobName = $"{folderPath}/{GuidGenerator.Create()}{extension}";
            await _blobManager.SaveAsync(input.BlobContainerName, blobName, input.Bytes);

            var blobDto = new BlobDto
            {
                BlobName = blobName,
                BlobContainerName = input.BlobContainerName,
                FileName = input.FileName
            };

            return blobDto;
        }

        public async Task<byte[]> GetAllBytesOrNullAsync(string blobContainerName, string blobName)
        {
            return await _blobManager.GetAllBytesOrNullAsync(blobContainerName, blobName);
        }
    }
}